using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ProtoGame.GameLib;
using ProtoGame.GameLib.Entities;
using System.Text;


namespace ProtoGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PGHUD : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        StringSprite mostradorTempo;
        StringSprite mostradorSaudeTxt;
        List<BasicSprite> mostradoresSaude;
        string fontPath;
        string saudePath;
        Texture2D saudeGfx;

        AtributosJogador atributos;

        bool IsRunning
        {
            get { return Visible && Enabled; }
            set
            {
                Enabled = value;
                Visible = value;
            }
        }

        public PGHUD(string saudePath, string fontPath, Game game)
            : base(game)
        {
            this.fontPath = fontPath;
            this.saudePath = saudePath;
            IsRunning = false;
        }

        public void WakeUp()
        {
            if (!IsRunning)
            {
                atributos = Game.Services.GetService(typeof(AtributosJogador)) as AtributosJogador;

                mostradoresSaude = new List<BasicSprite>(atributos.SaudeMax);
                for (int i = 0; i < atributos.SaudeMax; i++)
                    mostradoresSaude.Add(new BasicSprite(saudeGfx));

                IsRunning = true;
            }
        }

        public void GoSleep()
        {
            IsRunning = false;
            atributos = null;
            mostradoresSaude = null;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            SpriteFont fontMostrador = Game.Content.Load<SpriteFont>(fontPath);

            mostradorTempo = new StringSprite(fontMostrador);
            mostradorTempo.ColorEffects = Color.LightBlue;

            mostradorSaudeTxt = new StringSprite(fontMostrador);
            mostradorSaudeTxt.ColorEffects = Color.Yellow;
            mostradorSaudeTxt.Text = "Saude:";

            saudeGfx = Game.Content.Load<Texture2D>(saudePath);

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            mostradorTempo.Text = "Tempo: " + ((int)(atributos.TempoJogo.TotalSeconds)).ToString();
            mostradorTempo.Update(gameTime);

            for(int i = 0; i < atributos.SaudeMax; i++)
            {
                if (i + 1 <= atributos.Saude)
                {
                    mostradoresSaude[i].ColorEffects = Color.White;
                    mostradoresSaude[i].Scale = new Vector2(1f);
                }
                else
                {
                    mostradoresSaude[i].ColorEffects = Color.Gray;
                    mostradoresSaude[i].Scale = new Vector2(0.8f);
                }
            }

            PosicionarWidgets();

            base.Update(gameTime);
        }

        private void PosicionarWidgets()
        {
            Rectangle tela = new Rectangle(Game.GraphicsDevice.Viewport.X, Game.GraphicsDevice.Viewport.Y, Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height);
            Rectangle mostradorBounds = mostradorTempo.Bounds;

            // Top-left com um pequeno offset
            mostradorTempo.Position = new Vector2(
                tela.Width - mostradorBounds.Width - (tela.Width * 0.025f),
                tela.Width * 0.025f);

            Vector2 posMostradorSaude = new Vector2(tela.Width * 0.025f, tela.Height * 0.075f);
            mostradorSaudeTxt.Position = posMostradorSaude - new Vector2(0, mostradorSaudeTxt.Bounds.Height);
            posMostradorSaude.X += mostradorSaudeTxt.Bounds.Width;
            foreach (BasicSprite mostrador in mostradoresSaude)
            {
                mostrador.Position = posMostradorSaude - new Vector2(0,mostrador.Bounds.Height);
                posMostradorSaude.X += mostrador.Bounds.Width;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            mostradorTempo.Draw(spriteBatch);
            mostradorSaudeTxt.Draw(spriteBatch);
            foreach (BasicSprite mostrador in mostradoresSaude)
                mostrador.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}